package cn.iocoder.yudao.module.erp.service.business.vip;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.vip.vo.VipPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.vip.vo.VipSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.vip.VipDO;
import cn.iocoder.yudao.module.erp.dal.mysql.business.vip.VipMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.VIP_NOT_EXISTS;

/**
 * 客户级别 Service 实现类
 *
 * @author yy
 */
@Service
@Validated
public class VipServiceImpl implements VipService {

    @Resource
    private VipMapper vipMapper;

    @Override
    public Long createVip(VipSaveReqVO createReqVO) {
        // 插入
        VipDO vip = BeanUtils.toBean(createReqVO, VipDO.class);
        vipMapper.insert(vip);
        // 返回
        return vip.getId();
    }

    @Override
    public void updateVip(VipSaveReqVO updateReqVO) {
        // 校验存在
        validateVipExists(updateReqVO.getId());
        // 更新
        VipDO updateObj = BeanUtils.toBean(updateReqVO, VipDO.class);
        vipMapper.updateById(updateObj);
    }

    @Override
    public void deleteVip(Long id) {
        // 校验存在
        validateVipExists(id);
        // 删除
        vipMapper.deleteById(id);
    }

    private void validateVipExists(Long id) {
        if (vipMapper.selectById(id) == null) {
            throw exception(VIP_NOT_EXISTS);
        }
    }

    @Override
    public VipDO getVip(Long id) {
        return vipMapper.selectById(id);
    }

    @Override
    public PageResult<VipDO> getVipPage(VipPageReqVO pageReqVO) {
        return vipMapper.selectPage(pageReqVO);
    }

}